package tests {
	import assets.Asset;
	import assets.AssetManager;
	import blitting.SpriteSheet;
	import blitting.SpriteSheetAnimation;
	import blitting.SpriteSheetRenderer;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import views.Canvas;
	/**
	 * @author Charlie MacIsaac
	 */
	public class BlitVSClipTest {
		// PROPERTIES =============================================================================
		private var _testBlit : Boolean;
		private var _explosionsToCreate : uint;
		
		private var _stage : Stage;
		private var _canvas : Canvas;
		private var _profiler : Profiler;
		
		private var _assetManager : AssetManager;
		private var _explosionData : Asset;
		private var _explosionSheet : Asset;
		private var _explosions : Array;
		
		// GETTERS + SETTERS ======================================================================
		// CONSTRUCTOR ============================================================================
		public function BlitVSClipTest(stage : Stage, testBlit : Boolean = true, explosionsToCreate : uint = 1000) {
			_stage = stage;
			_stage.addEventListener(Event.FRAME_CONSTRUCTED, updatePositions, false, 0, true);
			_explosionsToCreate = explosionsToCreate;
			
			_profiler = new Profiler();
			
			var loadList : Array;
			if (testBlit) {
				_explosionData = new Asset("spritesheets/explosion.xml");
				_explosionSheet = new Asset("spritesheets/explosion.png");
				loadList = [_explosionSheet, _explosionData]
			} else {
				loadList = [new Asset("spriteclips/explosion.swf")];
			}
			
			_explosions = [];
			_assetManager = new AssetManager();
			
			_assetManager.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
			_assetManager.load(loadList);
		}
		
		// PUBLIC =================================================================================
		// PRIVATE ================================================================================
		private function generateLotsOfSpriteSheetExplosions() : void {
			_canvas = new Canvas(_stage, 0x000000);
			_stage.addChild(_canvas);
			_stage.addChild(_profiler);
			
			var explosionSpriteSheet : SpriteSheet = new SpriteSheet(_explosionSheet, _explosionData);
			var explosionRenderer : SpriteSheetRenderer = explosionSpriteSheet.getRenderer("base");
			var explosion : SpriteSheetAnimation;
			var columns : uint = Math.sqrt(_explosionsToCreate);
			var row : uint, column : uint;
			
			var test : BitmapData;
			var bitmap : Bitmap;
			for (var explosionIndex : uint = 0 ; explosionIndex < _explosionsToCreate ; explosionIndex++) {
				row = (explosionIndex / columns);
				column = explosionIndex - (row * columns);
				explosion = new SpriteSheetAnimation(explosionRenderer, true);
				explosion.frame = Math.random() * explosion.totalFrames;
				explosion.x = Math.random() * _stage.width;
				explosion.y = Math.random() * _stage.height;
				explosion.play();
				_canvas.addChild(explosion);
				_explosions.push(explosion);
			}
		}
		
		private function generateLotsOfSpriteClipExplosions() : void {
			_stage.addChild(_profiler);
			
			var explosionDefinition : Class = _assetManager.getClass("Explosion");
			var explosion : MovieClip;
			var columns : uint = Math.sqrt(_explosionsToCreate);
			var row : uint, column : uint;
			
			for (var explosionIndex : uint = 0 ; explosionIndex < _explosionsToCreate ; explosionIndex++) {
				row = (explosionIndex / columns);
				column = explosionIndex - (row * columns);
				explosion = new explosionDefinition() as MovieClip;
				explosion.gotoAndPlay(uint(Math.random() * explosion.totalFrames));
				explosion.x = Math.random() * _stage.stageWidth;
				explosion.y = Math.random() * _stage.stageHeight;
				_stage.addChild(explosion);
				_explosions.push(explosion);
			}
		}
		
		// LISTENERS ==============================================================================
		private function loadComplete(e : Event) : void {
			if (_explosionSheet && _explosionData)	generateLotsOfSpriteSheetExplosions();
			else									generateLotsOfSpriteClipExplosions();
		}
		
		private function updatePositions(e : Event) : void {
			var xAdjust : uint, yAdjust : uint;
			for each (var explosion : Object in _explosions) {
				xAdjust = Math.random() * 10;
				yAdjust = Math.random() * 10;
				
				if (explosion.x + xAdjust > _stage.stageWidth) explosion.x = -explosion.width * 0.5;
				if (explosion.y + yAdjust > _stage.stageHeight) explosion.y = -explosion.height * 0.5;
				
				explosion.x += xAdjust;
				explosion.y += yAdjust;
			}
		}
	}
}